﻿Imports System.Text.RegularExpressions
Imports System.Runtime.InteropServices

Public Class Web2Images


    

    Public Event Status(ByVal url As String, id As String)
    Public Delegate Sub ShowStatus(ByVal url As String, id As String)

    Public savename As String
    Public wurl As String
    Public path As String


    <DllImport("wininet.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function InternetGetCookie(lpszUrlName As String, lpszCookieName As String, lpszCookieData As String, ByRef lpdwSize As Long) As Boolean
    End Function

    Public Sub DoWork()


        WebBrowser1.ScriptErrorsSuppressed = True
        AddHandler WebBrowser1.DocumentCompleted, AddressOf WebBrowser1_DocumentCompleted

        Dim strCookieText As String
        Dim blnReturn As Boolean
        Dim lngCookie As Long 'cookie需要的长度

        blnReturn = InternetGetCookie(wurl, Nothing, strCookieText, lngCookie) 

        WebBrowser1.Navigate(wurl)
    End Sub



    Private Sub Web2Img(ByVal SaveAsName As String, ByVal path As String)
        Try

            Dim scrollWidth As Integer
            Dim scrollHeight As Integer
            scrollHeight = WebBrowser1.Document.Body.ScrollRectangle.Height
            scrollWidth = WebBrowser1.Document.Body.ScrollRectangle.Width
            WebBrowser1.Size = New Size(scrollWidth, scrollHeight)
            Dim bm As New Bitmap(scrollWidth, scrollHeight)
            '图片的高度达到一定范围才进行存储
            If (bm.Height > 400) Then

                If SaveAsName = String.Empty Then
                    SaveAsName = DateTime.Now.ToString() & ".jpg"
                End If
                If path = String.Empty Then
                    path = "e:\downpic\"
                End If



                WebBrowser1.DrawToBitmap(bm, New Rectangle(0, 0, bm.Width, bm.Height))



                '  SaveAsName = Regex.Replace(savename, "(\\|\/|\:|\*|\?|\""|\<|\>|\|)?", "")
                Dim badstrs As String() = New String() {"*", "?", "/", "\", ":", " "}

                For Each badstr As String In badstrs
                    SaveAsName = SaveAsName.Replace(badstr, "")
                Next

                If Not path.EndsWith("\") Then
                    path = path & "\"
                End If

                bm.Save(path & SaveAsName, Imaging.ImageFormat.Jpeg)
                Threading.Thread.Sleep(1000)

                bm.Dispose()
            End If

        Catch ex As Exception
            ' MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally

        End Try
        WebBrowser1.Stop()
        Dispose()

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        'RaiseEvent Status(wurl, savename)
        Dim WebBrowser1 As WebBrowser = sender

        '  If WebBrowser1.Document.Url.AbsoluteUri.IndexOf("res://") > -1 Then '出错处理
        AddHandler CType(sender, WebBrowser).Document.Window.Error, AddressOf Window_Error
        '   Threading.Thread.Sleep(1000)
        Web2Img("", "")
        '  End If




    End Sub

    Private Sub Window_Error(sender As Object, e As HtmlElementErrorEventArgs)
        ' Ignore the error and suppress the error dialog box. 
        e.Handled = True
    End Sub

   

End Class